package com.abu.manager.controller;


import com.abu.common.Resp;
import com.abu.manager.pojo.Manager;
import com.abu.manager.service.ManagerService;
import com.abu.manager.vo.ManagerVO;
import com.abu.manager.vo.QueryConditionVO;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 管理员表（超级管理员，楼主） 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2021-02-03
 */
@Slf4j
@RestController
@RequestMapping("/manager")
public class ManagerController {


    @Autowired
    private ManagerService managerService;

    @GetMapping("noAuthorize/id")
    public Resp getManagerByReviewerId(@RequestParam("reviewerId") String id) {
        Manager manager = this.managerService.getById(id);
        if (manager != null) {
            return Resp.ok().data("manager", manager);
        }
        return Resp.error().msg("管理员不存在").code(404);
    }

    @GetMapping("noAuthorize/managerInfo/all")
    public Resp getManagerInfoList() {
        List<Manager> managerList = this.managerService.selectInfoListBesidesSuperAdmin();
        if (!CollectionUtils.isEmpty(managerList)) {
            return Resp.ok().data("managerList", managerList);
        }
        return Resp.error().msg("数据为空").code(404);
    }

    @GetMapping("managerInfo/managerId")
    public Resp queryManagerInfoByManagerId() {
        ManagerVO managerVO = this.managerService.queryManagerInfoByManagerId();
        if (managerVO != null) {
            return Resp.ok().data("managerVO", managerVO);
        }
        return Resp.error().msg("数据不存在");
    }

    @PostMapping("noAuthorize/auth")
    public Resp auth(
            @RequestParam("mgName") String mgName,
            @RequestParam("mgPwd") String mgPwd) {
        HashMap<String, Object> data = this.managerService.auth(mgName, mgPwd);
        return Resp.ok().data(data);
    }

    @GetMapping("queryManagerByCondition")
    public Resp queryByCondition(QueryConditionVO queryCondition) {
        HashMap<String, Object> mapResult = this.managerService.queryByCondition(queryCondition);
        return Resp.ok().data(mapResult);
    }

    @PostMapping("addManager")
    public Resp addManager(@RequestBody ManagerVO manager) {
        Boolean flag = this.managerService.saveManagerAndRole(manager);
        if (flag) {
            return Resp.ok();
        }
        return Resp.error().msg("添加失败");
    }


    @PutMapping("updateManagerStatistics")
    public Resp updateManagerStatistics(@RequestBody Manager manager) {
        boolean flag = this.managerService.updateById(manager);
        if (!flag) {
            return Resp.error().msg("修改失败");
        }
        return Resp.ok();
    }

    @PutMapping("updateStatus")
    public Resp changeStatus(@RequestParam("status") Boolean status, @RequestParam(value = "managerId") Long managerId) {
        if (managerId == null) {
            return Resp.error().msg("数据为空");
        }
        UpdateWrapper<Manager> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("mg_id", managerId).set("mg_state", status);
        boolean flag = this.managerService.update(updateWrapper);
        Manager manager = this.managerService.getById(managerId);
        Boolean presentStatus = manager.getMgState();
        if (flag) {
            return Resp.ok().data("status", presentStatus);
        }
        return Resp.error().msg("修改状态失败");
    }

    @PutMapping("resetPassword")
    public Resp resetPwd(@RequestParam("mgId") Long mgId) {
        Boolean flag = this.managerService.resetPwd(mgId);
        if (flag) {
            return Resp.ok().msg("密码已重置为：aaa666");
        }
        return Resp.error().msg("重置失败");
    }


    @PutMapping("modifiedPwd")
    public Resp restPwd(@RequestBody ManagerVO managerVO) {
        Boolean flag = this.managerService.modifiedPwd(managerVO);
        if (flag) {
            return Resp.ok();
        }
        return Resp.error().msg("修改失败");
    }


    @DeleteMapping("deleteById")
    public Resp deleteManagerById(@RequestParam("managerId") Long managerId) {
        if (managerId == null) {
            return Resp.error().msg("数据为空");
        }
        boolean flag = this.managerService.removeManagerAndRole(managerId);
        if (!flag) {
            return Resp.error().msg("修改失败");
        }
        return Resp.ok();
    }

    @DeleteMapping("batchDelete")
    public Resp batchDelete(String ids) {
        Boolean flag = this.managerService.deleteBatch(ids);
        if (!flag) {
            return Resp.error().msg("删除失败");
        }
        return Resp.ok();
    }


}

